package com.ohuang.jiaxiaotong;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;

import android.view.View;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.example.myhttp.CallBackObject;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.Activity.LoginActivity;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;
import com.ohuang.jiaxiaotong.fragment.Class2Fragment;
import com.ohuang.jiaxiaotong.fragment.ClassFragment;
import com.ohuang.jiaxiaotong.fragment.Home2Fragment;
import com.ohuang.jiaxiaotong.fragment.HomeFragment;
import com.ohuang.jiaxiaotong.fragment.MsgFragment;
import com.ohuang.jiaxiaotong.fragment.MyFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;


public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private SparseArray<Fragment> sparseArray;
    private User user;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("首页");
        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("User");
        Ohuang.changeStatusBarTextImgColor(this, true);//将手机状态栏字体设置成黑色
        initView();//初始化
        EventBus.getDefault().register(this);//注册EventBus
        if (user!=null){
        EventBus.getDefault().postSticky(user);//发送粘性事件

        }
    }


    private void initView() {
        radioGroup = findViewById(R.id.tabs_rg_main);
        sparseArray = new SparseArray<>();
        if (user==null){
            user=new User(1,"admin","0",
                    "https://ss0.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=359996764,1645318124&fm=26&gp=0.jpg");
        }
        if ("1".equals(user.getRole())) {//家长端界面
            sparseArray.append(R.id.class_tab_main, new Class2Fragment());
            sparseArray.append(R.id.home_tab_main, new Home2Fragment());
            sparseArray.append(R.id.msg_tab_main, new MsgFragment());
            sparseArray.append(R.id.my_tab_main, new MyFragment());
        }else {//老师端界面
            sparseArray.append(R.id.class_tab_main, new ClassFragment());
            sparseArray.append(R.id.home_tab_main, new Home2Fragment());
            sparseArray.append(R.id.msg_tab_main, new MsgFragment());
            sparseArray.append(R.id.my_tab_main, new MyFragment());
        }
// 默认显示第一个
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
                sparseArray.get(R.id.home_tab_main)).commit();


        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {

            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    sparseArray.get(checkedId)).commit();
            switch (checkedId) {
                case R.id.class_tab_main:
                    setTitle("班级");
                    break;
                case R.id.home_tab_main:
                    setTitle("首页");
                    break;
                case R.id.msg_tab_main:
                    setTitle("消息");
                    break;
                case R.id.my_tab_main:
                    setTitle("我的");
                    break;
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void GetUserEventBus(EventMsg eventMsg) {
      if (eventMsg.getCode()==0){
          HttpGetUser();
      }
    }




//获取用户数据
    private void HttpGetUser() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",user.getUsername());
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/findUser", map, User.class, new CallBackObject<User>() {
            @Override
            public void success(User ojb) {
                user=ojb;
                if (user!=null){
                    EventBus.getDefault().postSticky(user);
                }
            }

            @Override
            public void fail(String s) {

            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
